HighCharts has a module to export generated charts to a file but it relies on an external server for rasterization and file download. This module, while using that one, uses HTML5 features and other JavaScript libraries to make it work without setting up an export server.
Current version: 1.1.4By using a combination of official export module, canvas-tools module (or canvg) and jsPDF, allow your users to export your charts client-side in SVG, PNG, JPEG or PDF, without privacy concerns.
It also supports pseudo-official export-csv to export raw data in CSV and XLS. All under a unified API. Check the dependency tree below for more information.
The dependency tree has been generated with HighCharts, and thus, is exportable!
Learn more on how to draw custom shapes with HighCharts renderer here.
With this configuration (all dependencies) and on this platform, those formats have been detected to be supported:
To install it, you need to do the following:
bower install highcharts-export-clientside
<script src="./lib/highcharts-export-clientside/highcharts-export-clientside.js"></script>
along-side the dependencies you need (see dependency tree above for more information).<!-- HighCharts --> <script src="./lib/highcharts/highcharts.js"></script> <!-- Other dependencies --> <script src="./lib/highcharts/modules/exporting.js"></script> <script src="./lib/highcharts/modules/canvas-tools.js"></script> <script src="./lib/export-csv/export-csv.js"></script> <script src="./lib/jspdf/dist/jspdf.min.js"></script> <!-- This module, after the dependencies --> <script src="./lib/highcharts-export-clientside/highcharts-export-clientside.js"></script>
If you just want to use the menu provided by HighCharts, you have nothing to do! Just include this module after the dependencies you need but before your chart definitions.
If you need to access the feature programmatically, the module adds a method
exportChartLocal(options, chartOptions)
to the
HighCharts.Chart
prototype. It attempts to mimic as closely as possible
exportChart(options, chartOptions)
provided by the official exporting module.
Quick snippet below:
$("#my-highcharts-container") .highcharts() // Fetches the Chart instance associated with this container. .exportChartLocal({ // All attributes are optionals (defaut type is "image/png"). type: Highcharts.exporting.MIME_TYPES.JPEG // For your convenience, MIME_TYPES are stored in an object. });
You may want to know if the current platform supports said export type. To do so, a function called
supports(mimeType)
exists.
if(Highcharts.exporting.supports(Highcharts.exporting.MIME_TYPES.XLS)) { // Excel file format is supported! }
That's all!
Fair. When possible, it first sets object a dummy URL in the HighCharts global configuration
(exporting.url
), just in case.
Next, it overrides official exportChart([…])
method with its own exportChartLocal([…])
so legacy code will still
work.
Finally, it changes click handlers in the menu with a custom ones. Again, just to be safe.
Hereunder, a list of defined objects and methods by this module.
HighCharts.Chart.prototype
:exportChartLocal(options, chartOptions)
chart.exportChart
.
options
(optional), an object containing export options. They overload those defined at chart definition.
Supported attributes and default values are as follow:
{ filename: "chart", chartOptions: {}, sourceWidth: <chart.width>, sourceHeight: <chart.height>, scale: 2, type: MIME_TYPES.PNG csv: { useBOM: MIME_TYPES.XLS, useLocalDecimalPoint: true, dateFormat: "%Y-%m-%d %H:%M:%S", itemDelimiter: ",", lineDelimiter: "\n", } }
filename
:
All Formats String, defaults to "chart"
.
Desired filename without the extension.options
and chartOptions
and will be binded to the current chart
instance. It will have to return a string, which will be the filename without its extension.Highcharts.exporting.USE_TITLE_FOR_FILENAME
has been defined
and that returns and lowercase variation of the chart title.
chartOptions
:
SVG, PNG, JPEG, PDF Object, defaults to undefined
.
Default chart options, see chartOptions
parameter.
sourceWidth
, sourceHeight
:
SVG, PNG, JPEG, PDF Integers, default to undefined
.
If not defined, takes the current width/height. Width / Height of exported before scale
is applied.
Superseeded by chart.width
and chart.height
if explicitly defined.
The unit is pixels except for PDF where it is millimeters.
scale
:
SVG, PNG, JPEG, PDF Integer, defaults to 2
.
Multiplying factor to the dimension of the exported image.
type
All Formats String, defaults to "image/png"
.
MIME type of the desired export format. See MIME_TYPES.*
.
csv.useBOM
:
CSV, XLS Unoffical
1.1.4
Boolean or String, defaults to MIME_TYPES.XLS
.
If true
or of the MIME type of the current export format, it will prepend the
UTF-8 BOM. This is unofficial and not compliant with export-csv
module but has been known to cause issues on accentuated when opening file in Microsoft Excel.
csv.useLocalDecimalPoint
:
CSV, XLS Unoffical
Boolean, defaults to true
.
If true
, exported files use the current browser decimal separator. Else uses .
(dot).
csv.dateFormat
:
CSV String, defaults to %Y-%m-%d %H:%M:%S
.
Format for the dates. A format understood by Highcharts.dateFormat([…])
.
csv.itemDelimiter
:
CSV String, defaults to ,
.
Character used to separate fields.
csv.lineDelimiter
:
CSV String, defaults to \n
.
Character used as new line.
chartOptions
(optional) is an object that will be passed through chart.getSVG(chartOptions)
.
It is used to overload rendering options prior to SVG generation. For instance, setting a different background,
changing legend position, etc.
exportChart(options, chartOptions)
exportChartLocal(options, chartOptions)
for legacy code use.
HighCharts.exporting
:MIME_TYPES
SVG
: image/svg+xml
PNG
: image/png
JPEG
: image/jpeg
PDF
: application/pdf
CSV
: text/csv
XLS
: application/vnd.ms-excel
USE_TITLE_FOR_FILENAME
exporting.filename
configuration option that return a
lowercase variation of the chart title. Spaces are replaced by underscores.
supports(mimeType)
mimeType
is expected to be a string. For available values, see MIME_TYPES
.
true
if the provided MIME type is supported.
This module has been tested under Google Chrome, Opera (webkit-based), Mozilla Firefox. and Internet Explorer 10-11.
IE10+ doesn't have JPEG support as it relies on msToBlob.
IE 9 and below are not supported at this stage.
This module is releasing under the Unlicense license. A copy of which can be found here.
Written with the help of the following resources: